<?php



namespace App\Http\Controllers\Api;


use App\Models\Activity;
use App\Models\ActivityApply;
use GatewayWorker\Lib\Gateway;
use Jiannei\Response\Laravel\Support\Facades\Response;

class ActivityController extends BasicController
{



    /**
     * @api {post} /activity/publish 活动发布
     * @apiName UserInfoLocation
     * @apiGroup UserInfo
     * @apiDescription
     * @apiHeader {String} Authorization='Bearer '
     */
    public function publish()
    {
        $data = $this->data(['title', 'image','intro','time']);
        $data['uid'] = $this->uid();
        $result = Activity::create($data);
        return Response::success($result ?? []);
    }
    /**
     * @api {post} /talent/apply 达人申请
     * @apiName UserInfoLocation
     * @apiGroup UserInfo
     * @apiDescription
     * @apiHeader {String} Authorization='Bearer '
     */
    public function apply()
    {
        $data = $this->data(['activity_id', 'mobile','name','wechat', 'email']);
        $act=Activity::find($data['activity_id']);
        if($act->status == 0 )
            return Response::fail('该活动已过期或已取消');
        $data['uid'] = $this->uid();
        $apply_info=ActivityApply::where(['uid'=>$data['uid'],'activity_id'=>$data['activity_id']])->first();
        if(empty($apply_info))  $result = ActivityApply::create($data);
        return Response::success($result ?? new \StdClass,'申请成功');
    }
    /**
     * @api {post} /talent/info 达人申请
     * @apiName UserInfoLocation
     * @apiGroup UserInfo
     * @apiDescription
     * @apiHeader {String} Authorization='Bearer '
     */
    public function list()
    {
        $applied=$this->request->input('applied');
        if($applied==1){
            $ids=ActivityApply::where(['uid'=>$this->uid()])->pluck('activity_id');
            $list=Activity::whereIn('id',$ids)->paginate();
        }else{
            $list=Activity::where(['status'=>1])->paginate();
            foreach ($list as $item){
                $apply=ActivityApply::where(['activity_id'=>$item->id,'uid'=>$this->uid()])->first();
                $item->applyed = empty($apply) ? 0 : 1;
            }
        }

        return Response::success($list);
    }
    /**
     * @api {post} /talent/info 达人申请
     * @apiName UserInfoLocation
     * @apiGroup UserInfo
     * @apiDescription
     * @apiHeader {String} Authorization='Bearer '
     */
    public function info()
    {
        $activity_id=$this->request->input('activity_id');
        $apply_info=Activity::where(['id'=>$activity_id])->with(['apply'=>function($query){
        $query->with(['user'=>function($query){
                $query->select(['id','name','avatar']);
            }]);
        }])->first();
        if(empty($apply_info)) return Response::fail();
        if($apply_info->uid!=$this->uid()){
            foreach ($apply_info->apply as $item){
                $item->mobile=substr_replace($item->mobile,'****',3,4);
            }
            $apply=ActivityApply::where(['activity_id'=>$activity_id,'uid'=>$this->uid()])->first();
            $apply_info->applyed = empty($apply) ? 0 : 1;
        }
        return Response::success($apply_info ?? []);
    }

}
